package martian.m4gic.display
{
	import flash.display.BitmapData;
	import flash.display.IBitmapDrawable;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class Buffer
	{
		private var fs:Vector.<BitmapData>;
			public function get frames():Vector.<BitmapData> { return fs; }
			public function get current():int { return i; }
			public function get amount():int { return fs.length; }
			public function get width():int { return fs[0].width; }
			public function get height():int { return fs[0].height; }
			
		private var i:int = 0;
		private var o:Point = new Point();
		
		public function Buffer(width:int, height:int, amount:int = 1, transparent:Boolean = false)
		{
			if (amount < 0) { amount = 0; }
			
			fs = new Vector.<BitmapData>;
			
			var a:int = 0;
			while (a < amount) { fs[a++] = new BitmapData(width, height, transparent, 0); }
		}
		
		public function capture(source:BitmapData, rect:Rectangle = null):void
		{
			if (rect == null) { rect = source.rect; }
			
			var f:BitmapData = fs.shift();
				f.copyPixels(source, rect, o);
			
			fs.push(f);
			
			if (i < fs.length - 1) { i++; }
			else { i = 0; }
		}
		
		public function dispose():void
		{
			for each(var f:BitmapData in fs)
			{
				f.dispose();
				f = null;
			}
			
			fs = null;
			o = null;
			i = 0;
		}
	}
}